<?php

namespace app\modules\controllers;


use app\modules\models\Admin;
use app\modules\models\Contract;
use app\modules\models\Order;
use app\modules\models\Parts;
use app\modules\models\Product;
use crazyfd\qiniu\Qiniu;
use Yii;
use yii\data\Pagination;
use yii\db\Query;
use yii\web\UploadedFile;

class OrderController extends CommonController
{ 
    protected $mustlogin = ['list', 'add', 'mod', 'del','jiedan','maintain','detail','ajaxlj','ajaxdel'];

    private $arr_status=array(
        1=>'已报修',
        2=>'维修中',
        3=>'维修结束',
    );

    public function actionList()
    {
        //print_r(Yii::$app->admin->identity->adminuser);

        $this->layout = "layout1";

        $question=Yii::$app->request->get('question');
        $status = Yii::$app->request->get('status');
        $adminid = Yii::$app->request->get('adminid');
        $where_str='true ';
        if(isset($question)&&!empty($question)){
            $where_str.="and question like '%$question%' ";
        }
        if(isset($status)&&$status!=4){
            $where_str.="and status=$status ";
        }
        if(isset($adminid)&&$adminid!=0){
            $where_str.="and adminid=$adminid ";
        }



        $model = Order::find()->where($where_str)->orderBy('id DESC');
        $count = $model->where($where_str)->count();
        $pageSize=$this->pagesize('ordersize');
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $data = $model->offset($pager->offset)->where($where_str)->limit($pager->limit)->all();
        $pageurl= $this->pageurl('order/list');
        $productlist = Product::getData();
        $adminlist=$this->getAdminList();
        foreach ($data as $k=>$v){
            $data[$k]->productid=$productlist[$v->productid]?:'无';
            if($v->orderTime!=0){
                $data[$k]->orderTime=date('Y-m-d H:i:s',$v->orderTime);
            }else{
                $data[$k]->orderTime='还没接单';
            }
            $data[$k]->statusname=$this->arr_status[$v->status];
            $data[$k]->adminname=$adminlist[$v->adminid]?:'无';
        }
        $adminlist=$this->getAdminList();
        $new_status=['4'=>'请选择状态']+$this->arr_status;
        $new_adminlist=['0'=>'请选择师傅']+$adminlist;
        return $this->render("orders", ['data' => $data, 'pager' => $pager,'total'=>$count,'pageurl'=>$pageurl,'pagesize' => $pageSize,'status'=>$new_status,'adminlist'=>$new_adminlist]);
    }

    public function actionAdd()
    {
        $model = new Order();
        $this->layout = "layout1";
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->add($post)) {
                //Yii::$app->session->setFlash("info", "添加成功");
                $this->redirect(['order/list']);
            }else{
                Yii::$app->session->setFlash("info", "添加失败");
            }
        }
        $adminlist=$this->getAdminList();
        $productlist = Product::getData();
        $contractlist = Contract::getData();
        $arr=array_keys($productlist);
        $partslist=Parts::getData($arr[0]);
        return $this->render("add", ['model' => $model,'productlist'=>$productlist,'contractlist'=>$contractlist,'adminlist'=>$adminlist,'title'=>'添加订单','partslist'=>$partslist]);
    }

    public function actionAjaxlj(){
        $post=Yii::$app->request->post();
        $partslist=Parts::getData($post['productid']);
        $arr=[];
        foreach ($partslist as $key=>$value){
            $arr[]=array(
                'id'=>$key,
                'pname'=>$value,
            );
        }
        echo json_encode($arr);
        die;
    }

    public function actionMod()
    {
        $this->layout = "layout1";
        $id = Yii::$app->request->get("id");
        $new_model = new Order();
        $model = $new_model->getOne($id);
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($new_model->updateDate($post,$id)) {
                return $this->redirect(['order/list']);
            }else{
                Yii::$app->session->setFlash("info", "修改失败");
            }
        }
        $adminlist=$this->getAdminList();
        $productlist = Product::getData();
        $contractlist = Contract::getData();
        $arr=array_keys($productlist);
        $partslist=Parts::getData($arr[0]);
        return $this->render('add', ['model' => $model,'productlist'=>$productlist,'contractlist'=>$contractlist,'adminlist'=>$adminlist,'title'=>'修改订单','partslist'=>$partslist]);
    }

    public function actionDel()
    {
        try {
            $id = Yii::$app->request->get('id');
            if (empty($id)) {
                throw new \Exception('参数错误');
            }
            if (!Order::deleteAll('id = :id', [":id" => $id])) {
                throw new \Exception('删除失败');
            }
            Yii::$app->session->setFlash('info', '删除成功');
        } catch(\Exception $e) {
            Yii::$app->session->setFlash('info', $e->getMessage());
        }
        return $this->redirect(Yii::$app->request->referrer);
    }


    public function actionAjaxdel(){
        $post = Yii::$app->request->post();
        if (!Order::deleteAll('id = :id', [":id" => $post['id']])) {
            echo 2;
        }else{
            echo 1;
        }
        die;
    }
    public function actionJiedan()
    {
        $id = Yii::$app->request->get("id");
        //$adminid=Yii::$app->admin->id;
        Order::updateAll(['status' => '2','orderTime'=>time()], 'id = :id', [':id' => $id]);
        return $this->redirect(Yii::$app->request->referrer);
    }

    public function actionMaintain(){
        $id = Yii::$app->request->get("id");
        $this->layout = "layout1";
        $new_model = new Order();
        $model = $new_model->getOne($id);
        $productlist = Product::getData();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($post['Order']['coverUrl']=='') {
                $model->addError('coverUrl', '维修图片不能为空');
            }
            if ($post['Order']['coverUrl']!='') {
                Order::updateAll(['coverUrl' =>$post['Order']['coverUrl'],'status'=>3], 'id = :id', [':id' => $post['Order']['id']]);
                return $this->redirect(['order/list']);
            } else {
                Yii::$app->session->setFlash('info', '订单维修没办法结束,您还没有上传维修图片');
            }
        }
        return $this->render('maintain', ['model' => $model,'productlist'=>$productlist,'status_arr'=>$this->arr_status,'title'=>'结束订单']);
    }



    public function actionDetail(){
        $id = Yii::$app->request->get("id");
        $this->layout = "layout1";
        $new_model = new Order();
        $model = $new_model->getOne($id);
        $productlist = Product::getData();
        return $this->render('detail', ['model' => $model,'productlist'=>$productlist,'status_arr'=>$this->arr_status,'title'=>'订单详情']);
    }


    private function getAdminList(){
        $auth = Yii::$app->authManager;
        $arr_admin=(new Query())->from($auth->assignmentTable)->where("item_name = 'master'")->all();
        $admin_model=new Admin();
        $adminlist=$admin_model->getAdminAll();
        $new_admin=[];
        foreach ($arr_admin as $k=>$v){
            $new_admin[$v['user_id']]=$adminlist[$v['user_id']];
        }
        return $new_admin;
    }

}